<?xml version="1.0" encoding="UTF-8"?><d:tdl xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.backbase.com/2006/btl" xmlns:bb="http://www.backbase.com/2006/client"  xmlns:d="http://www.backbase.com/2006/tdl" >

	<d:namespace name="http://www.backbase.com/2006/btl">

		<d:uses interface="iDataCommunicator" src="dataBinding.xml"/>

		<d:behavior name="remoteData" implements="b:iDataCommunicator">
			

			

			

			<d:attribute name="dataType" default="application/xml">
				
				<d:mapper type="text/javascript"><![CDATA[
					btl.dataBinding.setType(this, value);
				]]></d:mapper>
			</d:attribute>

			<d:attribute name="cacheLimit" default="1000">
				
			</d:attribute>

			<d:attribute name="requestType" default="application/json">
				
			</d:attribute>

			<d:attribute name="dataSelect">
				
			</d:attribute>

			<d:attribute name="useTimestamp" default="false">
				
			</d:attribute>

			<d:attribute name="method" default="POST">
				
			</d:attribute>

			<d:attribute name="url">
				
			</d:attribute>

			<d:attribute name="asynchronous" default="true">
				
			</d:attribute>

			<d:property name="user">
				
				<d:getter type="text/javascript"><![CDATA[
					return this._['_'+name] ? this._['_'+name] : '';
				]]></d:getter>
			</d:property>

			<d:property name="password">
				
				<d:getter type="text/javascript"><![CDATA[
					return this._['_'+name] ? this._['_'+name] : '';
				]]></d:getter>
			</d:property>

			<d:property name="url">
				
				<d:getter type="text/javascript"><![CDATA[
					return bb.uri.resolveUri(this.getAttribute(name), this.getProperty('baseURI'));
				]]></d:getter>
				<d:setter type="text/javascript"><![CDATA[
					this.setAttribute(name, value);
				]]></d:setter>
			</d:property>

			<d:property name="urlQuery">
				
			</d:property>
			
			<d:method name="sendRequest">
				<d:argument name="action"/>
				<d:argument name="observer"/>
				<d:body type="text/javascript"><![CDATA[
					if (!this.dataType)
						return;
					var oXHR = new XMLHttpRequest;
					var oEvent = bb.document.createEvent('Events');
					oEvent.initEvent('dataRequest', false, true);
					oEvent.XMLHttpRequest = oXHR;
					oEvent.action = action;
					if (this.dispatchEvent(oEvent)) {
						var oAction = oEvent.action;

						// Add serialized delta as parameter

						var sRequestType = this.getAttribute('requestType');
						if (!sRequestType || !sRequestType.length)
							sRequestType = this.dataType;

						var oActionGroup = new btl.dataBinding.ActionGroup();
						oActionGroup.addAction(oAction);
						var sData = encodeURIComponent(btl.dataSource.types.getType(sRequestType).serialize(oActionGroup));
						var sMethod = this.getAttribute('method').toUpperCase();
						if (oAction instanceof Object && (sMethod != 'POST' || sRequestType == 'application/json' || sRequestType == 'text/json'))
							sData = 'request=' + sData;

						var aQuery = [];
						if (oAction instanceof Object) {
							aQuery[aQuery.length] = 'action=' + encodeURIComponent(oAction.type);
							var hAttributes = oAction.getAttributeMap();
							for (var sAttrName in hAttributes) {
								if (hAttributes.hasOwnProperty(sAttrName)) {
									var vAttribute = hAttributes[sAttrName];
									if (typeof vAttribute != 'object')
										aQuery[aQuery.length] = encodeURIComponent(sAttrName) + '='
														 + encodeURIComponent(vAttribute);
								}
							}
						}
						var oSource = this;
						var bJSON = this.dataType == "application/json" || this.dataType == "text/json";

						oXHR.onreadystatechange = function() {
							if (oXHR.readyState == 4){
								if (!oXHR.aborted) {
									var oEvent = bb.document.createEvent('Events');
									if (oXHR.status == 0 || (oXHR.status >= 200 && oXHR.status < 300)) {
										// Dispatch dataResponse event for backward compatiblity with 4.0.2
										oEvent.initEvent('dataResponse', false, true);
										oEvent.XMLHttpRequest = oXHR;
										oEvent.data = oXHR.responseText;
										if (oSource.dispatchEvent(oEvent)) {
											if (oEvent.data && oEvent.data.length) {
												if (!bJSON && oSource.hasAttribute('dataSelect')) { //unserialize and run XPath
													var oDoc = bb.xml.parse(oEvent.data, true);
													var oResponseAction = new btl.dataBinding.Action('read', null);
													if (oDoc) {
														var aNodes = bb.xml.selectNodes(oDoc, oSource.getAttribute('dataSelect'), oSource.modelNode);
														var oResponseRecordList = oSource.typeHandler.unserializeRecordList(aNodes, btl.dataSource.getDataField(oSource, 'identifier'));
													} else {
														var oResponseRecordList = new btl.dataBinding.RecordList();
														oResponseAction.setAttribute('error', "Can't parse the response.");
													}
													oResponseAction.setRecords(oResponseRecordList);
													var oResponseActionGroup = new btl.dataBinding.ActionGroup();
													oResponseActionGroup.addAction(oResponseAction);
													oSource.acceptData(oResponseActionGroup, observer);
												} else
													oSource.acceptRawData(oEvent.data, observer);
											} else
												btl.dataBinding.fireErrorEvent(oSource, 0, 'Empty server response');
										}
									} else {
										oEvent.initEvent('error', true, false);
										oEvent.XMLHttpRequest = oXHR;
										oEvent.message = oXHR.statusText;
										oEvent.code = oXHR.status;
										oSource.dispatchEvent(oEvent);
										bb.command.trace(oSource, 'Error "' + oXHR.status + ' ' + oXHR.statusText + '" while loading file ' + sUrl, 2);
									}
								}
								btl.dataSource.requestQueueContinue(oSource);
							}
						};

						var aHeaders = {};
						var sUrl = this.getProperty('url');
						var oQuery = this.getProperty('urlQuery');
						if( oQuery){
							for(var sKey in oQuery)
								if (oQuery.hasOwnProperty(sKey))
									aQuery[ aQuery.length] = encodeURIComponent(sKey) + '=' + encodeURIComponent( oQuery[sKey]);
						}
						aQuery[ aQuery.length] = 'dataType=' + encodeURIComponent(this.dataType);
						if( sRequestType != this.dataType)
							aQuery[ aQuery.length] = 'requestType=' + encodeURIComponent( sRequestType);

						if( this.getAttribute('useTimestamp') != 'false')
							aQuery[ aQuery.length] = 'timestamp=' + encodeURIComponent( (new Date()).getTime());


						var sQuery = aQuery.length ? aQuery.join('&') : null; // request attributes

						if(sData && (sMethod == 'GET' || sMethod == 'HEAD')) {
							sUrl = sUrl + ( sUrl.indexOf('?') < 0 ? '?' : '&' ) + sData;
							sData = null;
						} else if(sMethod == 'POST' && !aHeaders['Content-Type']) //Set the default Content-Type header for POST if it's not yet set
							aHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
						if( sQuery)
							sUrl = sUrl + ( sUrl.indexOf('?') < 0 ? '?' : '&' ) + sQuery;

						//Set the default Accept header if not already set
						if(!aHeaders['Accept'])
							aHeaders['Accept'] = this.dataType;
						oXHR.open(sMethod, sUrl, btl.isTrueValue('asynchronous', this.getAttribute('asynchronous')),
								  this.getProperty('user'), this.getProperty('password'));
						for(var i in aHeaders)
							if(typeof aHeaders[i] != 'function')
								oXHR.setRequestHeader(i, aHeaders[i]);
						oXHR.send(sData);
						return oXHR;
					}
				]]></d:body>
			</d:method>

			<d:method name="cancelRequest">
				
				<d:argument name="request">
				
				</d:argument>
				<d:body type="text/javascript"><![CDATA[
					if(request){
						request.aborted = true;
						request.abort();
					}
				]]></d:body>
			</d:method>
		</d:behavior>
	</d:namespace>
</d:tdl>